function qsToJson(query) {
    let obj = {}
    let s1 = query.substring(1)
    let arr1 = s1.split('&')   // a=11,b=22,c=33
    arr1.forEach(value => {
        let arr2 = value.split('=')//  a,11   b,22
        obj[arr2[0]] = decodeURIComponent(arr2[1])
    })
    return obj
}

let data = qsToJson(location.search)
let filterData = { page: 1, keyword: qsToJson(location.search).keyword }
async function searchByKeyword({ page, keyword = "" }) {
    let { result: result6 } = await myAjax({
        url: '/api/search',
        data: {
            keyword
        }
    })
    let strs = ''
    let str1 = `搜索到 <span>${qsToJson(location.search).keyword}</span> 关键字共<span>${result6.result.rows}</span> 条，共<span>${result6.result.totalPage}</span>页</div>`
    result6.result.data.forEach(element => {
        strs += `
        <li>
        <div class="top">
        <a href="videoDetail.html?id=${element.cid}">
            <img src="${element.image_src}" alt="" class="m">
            <p>学科：${element.subject_name}</p>
        </div>
        <div class="bottom">
            <div class="left">
                <span>${element.type_name}${element.subject_name}${element.title}</span>
                <span class="time">地区：${element.area_name}</span>
            </div>
            <div class="right">${element.price == 0 ? '免费学习' : element.price}</div>
        </div>
    </li>
        `
    })

    let nextPage = page + 1 //下一页
    let prevPage = page - 1 //上一页

    if (prevPage <= 1) {
        prevPage = 1
    }

    //页码按钮拼接
    let pageStr = ''
    pageStr += `<li v="${prevPage}">上一页</li>`
    for (let i = 1; i <= result6.result.totalPage; i++) {
        if (i == page) {//如果当前的page和i相同代表当前页
            pageStr += `<li v="${i}" class="active">${i}</li>`
        } else {
            pageStr += `<li v="${i}">${i}</li>`
        }
    }
    pageStr += `<li v="${nextPage}">下一页</li>`
    $('#search_str').html(strs)
    $('#searcha').html(str1)
    $('#pageSTR').html(pageStr)

    $('#pageSTR').on('click', 'li', function () {
        let page = $(this).attr('v') - 0
        filterData.page = page
        searchByKeyword(filterData)
    })

}
searchByKeyword(filterData)




